﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Ycjw.PipeLine.Map.Tools.Conditions.ProfileAnalyst
{
    public partial class ProfileAnalystFormNew : Sunny.UI.UIForm
    {
        double m_LineMax = -999;
        double m_LineMin = 999;
        public bool WestToEast { get; set; }
        int marginDefault = 32;
        Graphics m_Graphics;
        Pen defaultPen;
        Brush defaultBrush;
        Pen penWS;
        Pen penYS;
        Pen penWSLine;
        Pen penYSLine;

        Font fontDefault;
        Font fontInner;

        Bitmap m_Bitmap;

        double MaxX = -180;
        double MaxY = -90;
        double MinX = 180;
        double MinY = 90;
        public ProfileAnalystFormNew()
        {
            InitializeComponent();
            this.uiDataGridViewStatics.BackgroundColor = this.panelDraw.BackColor;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            InitMaxMinPosition();
            InitMaxMinDepth();

            m_Graphics = this.panelDraw.CreateGraphics();
            SetGraphics(m_Graphics);
            defaultPen = Pens.Black;
            defaultBrush = Brushes.Black;
            fontDefault = new Font("yahei", 14);
            fontInner = new Font("雅黑",7);
            penWS = new Pen(Color.IndianRed,2f);
            penYS = new Pen(Color.BlueViolet,2f);

            penWSLine = new Pen(Color.IndianRed, 1f) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash};
            penYSLine = new Pen(Color.BlueViolet, 1f) { DashStyle = System.Drawing.Drawing2D.DashStyle.Dash };

            Bitmap bm = crop(m_Bitmap);
            this.m_pictureBoxMap.Image = bm;
        }

        void SetGraphics(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高，即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
        }

        private Bitmap crop(Bitmap src)
        {
            var paddingBoth = src.Width / 5;
            Rectangle cropRect = new Rectangle(0, src.Height / 3, src.Width , src.Height / 5);
            Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(src, new Rectangle(paddingBoth, 0, target.Width- paddingBoth, target.Height),
                      cropRect,
                      GraphicsUnit.Pixel);
            }
            return target;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            DrawXLine();
            DrawXText();
            DrawYLine();
            DrawYText();
            DrawPointWithLine();
            DrawLegend();
        }
        void InitMaxMinPosition()
        {
            foreach (var xx in m_Positions)
            {
                foreach (var x in xx)
                {
                    if (x > MaxX) MaxX = x;
                    if (x < MinX) MinX = x;
                }
            }
            var dif = MaxX - MinX;
            if (m_LineMax > MaxX) MaxX = m_LineMax;
            if (m_LineMin < MinX) MinX = m_LineMin;
            MaxX = MaxX + dif / 10.0;
            MinX = MinX - dif / 10.0;


        }
        void InitMaxMinDepth()
        {
            foreach (var dd in m_Depths)
            {
                foreach (var d in dd)
                {
                    if (d > MaxY) MaxY = d;
                    if (d < MinY) MinY = d;
                }
            }
            var dif = MaxY - MinY;
            if (dif == 0) dif = 1;
            MaxY += dif / 10.0;
            MinY -= dif / 10.0;
        }
        private List<List<double>> m_Positions = new List<List<double>>();
        public List<List<double>> Positions
        {
            get { return m_Positions; }
            set
            {
                m_Positions = value;
            }
        }
        private List<List<double>> m_Depths = new List<List<double>>();
        public List<List<double>> Depths
        {
            get { return m_Depths; }
            set
            {
                m_Depths = value;
            }
        }

        private List<List<double>> m_Radiuses = new List<List<double>>();

        public Bitmap Bitmap { get => m_Bitmap; set => m_Bitmap = value; }
        public double LineMax { get => m_LineMax; set => m_LineMax = value; }
        public double LineMin { get => m_LineMin; set => m_LineMin = value; }
        public List<List<double>> Radiuses { get => m_Radiuses; set => m_Radiuses = value; }

        void DrawXLine()
        {
            var p1 = new Point(marginDefault, this.panelDraw.Height - marginDefault);
            var p2 = new Point(this.panelDraw.Width - marginDefault, this.panelDraw.Height - marginDefault);
            m_Graphics.DrawLine(defaultPen, p1, p2);
        }
        void DrawYLine()
        {
            var p0 = new Point(marginDefault, marginDefault);
            var p1 = new Point(marginDefault, this.panelDraw.Height - marginDefault);

            var p2 = new Point(this.panelDraw.Width - marginDefault, marginDefault);
            var p3 = new Point(this.panelDraw.Width - marginDefault, this.panelDraw.Height - marginDefault);

            m_Graphics.DrawLine(defaultPen, p0, p1);
            m_Graphics.DrawLine(defaultPen, p2, p3);
        }
        //小数转度分秒，如输入33.59108333，返回33.35279（表示33度35分27.9秒）
        private string Double2DFM(double temp)
        {
            int d = (int)Math.Floor(temp);    //33   //返回小于或等于指定数字的最大整数
            double x = (temp - d) * 60;       //0.59108333*60=35.464998(分)
            int m = (int)Math.Floor(x);       //35
            double s = (x - m) * 60;          //0.464998*60=27.89988
            var result = d+"°" + m / 100 + "'" + (s / 100.0).ToString("F2") + "\"";
            return result;
        }
        void DrawXText()
        {
            var strMinX = Double2DFM(MinX);
            var strMaxX = Double2DFM(MaxX);
            var p1 = new Point(marginDefault, this.panelDraw.Height - marginDefault + 8);
            var p2 = new Point(this.panelDraw.Width - marginDefault - (int)this.m_Graphics.MeasureString(strMaxX, fontDefault).Width, this.panelDraw.Height - marginDefault + 8);

            m_Graphics.DrawString(strMinX, fontDefault, defaultBrush, p1);
            m_Graphics.DrawString(strMaxX, fontDefault, defaultBrush, p2);
        }
        void DrawYText()
        {
            var p0 = new Point(marginDefault, marginDefault);
            var p2 = new Point(this.panelDraw.Width - marginDefault*2, marginDefault);
            string y1Str = "西", y2Str = "东";
            if (!WestToEast)
            {
                y1Str = "北";
                y2Str = "南";
            }

            m_Graphics.DrawString(y1Str, fontDefault, defaultBrush, p0);
            m_Graphics.DrawString(y2Str, fontDefault, defaultBrush, p2);
        }
        void DrawLegend()
        {
            float r = 8.0f;
            m_Graphics.DrawEllipse(penWS, (float)marginDefault * 2, r * 2 / 3, r, r);
            m_Graphics.DrawString("污水管线", fontInner, defaultBrush, (float)marginDefault * 2 + r, 2 * r / 3);

            m_Graphics.DrawEllipse(penYS, (float)marginDefault + 120, r * 2 / 3, r, r);
            m_Graphics.DrawString("雨水管线", fontInner, defaultBrush, (float)marginDefault + 120 + r, 2 * r / 3);
        }

        double GetR(double r)
        {
            var index = 0;
            for(int i = 0;i < m_Radiuses.Count; i++)
            {
                for(int j = 0;j < m_Radiuses[i].Count; j++)
                {
                    var tmpR = m_Radiuses[i][j];
                    if (tmpR > r) index+=2;
                }
            }
            return index;
        }

        public void SetDataTable(DataTable dt)
        {
            for(int i = dt.Columns.Count -1; i >=0; i--)
            {
                var allNull = true;
                for(int j = 0;j < dt.Rows.Count; j++)
                {
                    var obj = dt.Rows[j][i];
                    if (obj !=null && !string.IsNullOrEmpty(obj.ToString()))
                    {
                        allNull = false;
                    }
                }
                if (allNull)
                {
                    dt.Columns.RemoveAt(i);
                }
            }
            uiDataGridViewStatics.DataSource = dt;
        }
        void DrawPointWithLine()
        {
            for (var i = 0; i < m_Positions.Count; i++)
            {
                var pp = m_Positions[i];
                var vv = m_Depths[i];
                var rr = m_Radiuses[i];
                for (var j = 0; j < pp.Count; j++)
                {
                    var p = pp[j];
                    var v = vv[j];
                    var theR = rr[j];
                    var x = (p - MinX) * (this.panelDraw.Width - marginDefault * 2) / ((MaxX - MinX));
                    var xPos = marginDefault + x;

                    var y = (v - MinY) * (this.panelDraw.Height - marginDefault * 2) / (MaxY - MinY);
                    var yPos = marginDefault + y;
                    Pen thePen,thePenLine;
                    if (i == 0)
                    {
                        thePen = penWS;
                        thePenLine = penWSLine;
                    }
                    else
                    {
                        thePen = penYS;
                        thePenLine = penYSLine;
                    }
                    float r = 8.0f;
                    r += (float)GetR(theR);
                    m_Graphics.DrawEllipse(thePen, (float)xPos, (float)yPos, r, r);
                    m_Graphics.DrawLine(thePenLine, (float)xPos + r / 2, (float)yPos, (float)xPos + r / 2, marginDefault);

                    var strInfo = "埋深:" + v;
                    var strWidth = m_Graphics.MeasureString(strInfo, fontInner).Width;
                    m_Graphics.DrawString(strInfo, fontInner, defaultBrush, (float)xPos + r - strWidth / 2, (float)yPos + r * 3 / 2);
                }
            }
        }
    }
}
